package pl.maciejziarko.mshare.web.rest;

import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pl.maciejziarko.mshare.domain.Resource;
import pl.maciejziarko.mshare.dto.ResourceDTO;
import pl.maciejziarko.mshare.dto.converters.ResourceDTOConverter;
import pl.maciejziarko.mshare.service.ResourceService;
import pl.maciejziarko.mshare.service.exception.InvalidResourceException;
import pl.maciejziarko.mshare.service.exception.ResourceNotFoundException;
import pl.maciejziarko.mshare.service.exception.ResourceServiceException;

/**
 * @author Maciej Ziarko
 */
@Controller
@RequestMapping(value = "/api/resource")
public class ResourceRESTWebService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceRESTWebService.class);

    private ResourceService resourceService;

    private ResourceDTOConverter converter;

    @Autowired
    public ResourceRESTWebService(ResourceService resourceService,
                                  ResourceDTOConverter converter) {
        this.resourceService = resourceService;
        this.converter = converter;
    }

    @RequestMapping(value = "/{id}",
            method = RequestMethod.GET,
            headers = {"Accept=application/xml"})
    public
    @ResponseBody
    ResourceDTO getResource(@PathVariable long id) throws ResourceServiceException, ResourceNotFoundException {
        return converter.convert(resourceService.getResource(id));
    }

    @RequestMapping(method = RequestMethod.DELETE)
    void removeAllResources() throws ResourceServiceException {
        resourceService.removeAllResources();
    }

    @RequestMapping(method = RequestMethod.POST,
            headers = {"Content-Type=application/xml", "Accept=application/xml"})
    @ResponseStatus(HttpStatus.CREATED)
    public
    @ResponseBody
    ResourceDTO addResource(@RequestBody ResourceDTO resourceDTO) throws ResourceServiceException, InvalidResourceException {
        //Get resource from form:
        Resource resource = converter.convert(resourceDTO);

        //Add:
        resourceService.addResource(resource);

        //Return dto:
        ResourceDTO returnResourceDTO = new ResourceDTO(resource.getId());
        returnResourceDTO.setSize(resource.getSize());
        return returnResourceDTO;
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Resource not found!")
    public void handleResourceNotFoundException() {
        //Empty
    }

    @ExceptionHandler(InvalidResourceException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Invalid resource!")
    public void handleInvalidResourceException() {
        //Empty
    }

    @ExceptionHandler(ResourceServiceException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Service error occurred!")
    public void handleResourceServiceException() {
        //Empty
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED, reason = "Unauthorized!")
    public void handleAuthorizationException() {
        //Empty
    }
}
